package com.gdth.bank.presenter;

import java.io.File;
import java.util.Date;
import java.util.List;

import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

import android.os.Environment;
import android.os.Handler;
import android.os.Message;

import com.gdth.bank.BankApplication;
import com.gdth.bank.bean.DownloadBean;
import com.gdth.bank.model.CjkhFjxxModel;
import com.gdth.bank.model.DownloadModel;
import com.gdth.bank.model.SynchronizeModel;
import com.gdth.bank.view.CjkhFjxxView;
import com.gdth.database.bean.TAppFjxxb;
import com.gdth.utils.ToolUtil;
import com.gdth.widget.dialog.BaseToast;

public class CjkhFjxxPresenter {
	private CjkhFjxxView mCjkhFjxxView;
	private CjkhFjxxModel mCjkhFjxxModel;
	private SynchronizeModel mSynchronizeModel;
	private int fjSize,i=0;
	private String strs="";
	
	public CjkhFjxxPresenter(CjkhFjxxView mCjkhFjxxView) {
		this.mCjkhFjxxView = mCjkhFjxxView;
		this.mCjkhFjxxModel = new CjkhFjxxModel();
		this.mSynchronizeModel =new SynchronizeModel();
	}
	
	/**
	 * 获取所有附件
	 * @param jcuuid
	 */
	public void getFjxxByUuid(String jcuuid){
		mCjkhFjxxModel.getFjxxByUuid(jcuuid)
		.subscribeOn(Schedulers.io())
		.unsubscribeOn(Schedulers.io())
		.observeOn(AndroidSchedulers.mainThread())
		.subscribe(new Subscriber<List<TAppFjxxb>>(){

			@Override
			public void onCompleted() {
				// TODO Auto-generated method stub
			}

			@Override
			public void onError(Throwable arg0) {
				// TODO Auto-generated method stub
				mCjkhFjxxView.showMessage("获取失败");
			}

			@Override
			public void onNext(List<TAppFjxxb> arg0) {
				// TODO Auto-generated method stub
				mCjkhFjxxView.getAllFjxx(arg0);
			}
		});
	}
	
	public void save(TAppFjxxb mTAppFjxxb) {
		mCjkhFjxxView.showProgressDialog("正在保存...");
		
		mCjkhFjxxModel.saveCjkhFjxx(mTAppFjxxb)
			.subscribeOn(Schedulers.io())
			.unsubscribeOn(Schedulers.io())
			.observeOn(AndroidSchedulers.mainThread())
			.subscribe(new Subscriber<Long>(){

				@Override
				public void onCompleted() {
					// TODO Auto-generated method stub
					mCjkhFjxxView.hideProgressDialog();
					mCjkhFjxxView.showMessage("保存成功");
				}

				@Override
				public void onError(Throwable arg0) {
					// TODO Auto-generated method stub
					mCjkhFjxxView.hideProgressDialog();
					mCjkhFjxxView.showMessage(arg0.getMessage());
				}

				@Override
				public void onNext(Long arg0) {
					// TODO Auto-generated method stub
					mCjkhFjxxView.saveSuccess(arg0);
				}
			});
	}
	
	public void getFjxxuuid(final String uuid){
		mCjkhFjxxView.showProgressDialog("上传附件");
		mCjkhFjxxModel.getFjxxuuid(uuid)
		.subscribeOn(Schedulers.io())
		.unsubscribeOn(Schedulers.io())
		.observeOn(AndroidSchedulers.mainThread())
		.subscribe(new Subscriber<List<TAppFjxxb>>(){

			@Override
			public void onCompleted() {
				// TODO Auto-generated method stub
//				mCjkhFjxxView.hideProgressDialog();
				
			}

			@Override
			public void onError(Throwable arg0) {
				// TODO Auto-generated method stub
				mCjkhFjxxView.showMessage(arg0.getMessage());
				mCjkhFjxxView.hideProgressDialog();
			}

			@Override
			public void onNext(List<TAppFjxxb> arg0) {
				// TODO Auto-generated method stub
				fjSize=arg0.size();
				strs=uuid;
				for (TAppFjxxb mTAppFjxxb : arg0) {
				//TAppFjxxb mTAppFjxxb= arg0.get(0);
				DownloadBean bean = new DownloadBean();
				bean.setName(mTAppFjxxb.getWjmc());
				bean.setWjlx(mTAppFjxxb.getWjlx());
				bean.setUuid(mTAppFjxxb.getUuid());
				bean.setWybs(mTAppFjxxb.getWybs());

				String wjlx=mTAppFjxxb.getWjmc();
				String wjlxx = wjlx.substring(wjlx.lastIndexOf(".") + 1, wjlx.length());
				String filePath =ToolUtil.getFilePath(mTAppFjxxb.getUuid() +"."+wjlxx);
				if(!filePath.startsWith(Environment.getExternalStorageDirectory().toString())){
					filePath = Environment.getExternalStorageDirectory() + filePath;
				}
				File file = new File(filePath);
				if(file.exists()){
					bean.setLocalPath(filePath);
				}else{
					String appdzStr = mTAppFjxxb.getAppdz();
					if(appdzStr.length() > 0){
						if(!appdzStr.startsWith(Environment.getExternalStorageDirectory().toString())){
							appdzStr = Environment.getExternalStorageDirectory() + appdzStr;
						}
						File fileDz = new File(appdzStr);
						if(fileDz.exists()){
							bean.setLocalPath(appdzStr);
						}
					}
				}

				bean.setSqrsfz(mTAppFjxxb.getSfz());
				bean.setScms(0);
				bean.setCjsj(new Date());
				bean.setGnmk(1);
				if(null == mTAppFjxxb.getFjlx()){
					bean.setFjlx(5);
				}else{
					bean.setFjlx(mTAppFjxxb.getFjlx());
				}
				//if ()
				bean.setFjlx(mTAppFjxxb.getFjlx());
				DownloadModel.getInstance().uploadFJ(bean, handler);
			}
			}
		});
	}
Handler handler = new Handler(new Handler.Callback() {
		
		@Override
		public boolean handleMessage(Message msg) {
			// TODO Auto-generated method stub
			switch (msg.what) {
				case 101:
					String str=msg.getData().getString("content");
					String message = msg.getData().getString("messageValue");
					if(fjSize==1 && !strs.equals("")){
						if(!str.equals("")){
							mCjkhFjxxView.hideProgressDialog();
							mCjkhFjxxView.showMessage("上传成功");
							mCjkhFjxxView.saveSuccess(0);
						}else{
							mCjkhFjxxView.hideProgressDialog();
							mCjkhFjxxView.showMessage("上传失败");
						}
					}else if(fjSize>1){
						i++;
						if(str.equals("")){
							mCjkhFjxxView.hideProgressDialog();
							mCjkhFjxxView.showMessage("上传失败");
							return false;
							}
						if(i==fjSize){
							mCjkhFjxxView.hideProgressDialog();
							mCjkhFjxxView.showMessage("上传成功");
//							mCjkhFjxxView.saveSuccess(1);
						}
						
					}else if(fjSize==1 && strs.equals("")){
						mCjkhFjxxView.showMessage("上传成功");
						mCjkhFjxxView.hideProgressDialog();
//						mCjkhFjxxView.saveSuccess(1);
					}
					mCjkhFjxxView.saveSuccess(1);
					break;
				case 102:
					mCjkhFjxxView.hideProgressDialog();
					mCjkhFjxxView.showMessage("上传失败");
					mCjkhFjxxView.saveSuccess(1);
					break;
				case 103:
					String value = msg.getData().getString("errorValue");
					mCjkhFjxxView.hideProgressDialog();
					mCjkhFjxxView.showMessage("上传失败"+value);
					mCjkhFjxxView.saveSuccess(1);
					break;
			}
			
			return false;
		}
	});

public void panduanWl(final String str){
	mSynchronizeModel.getWl("")
	.subscribeOn(Schedulers.io())
	.unsubscribeOn(Schedulers.io())
	.observeOn(AndroidSchedulers.mainThread()) //AndroidSchedulers.mainThread()
	.subscribe(new Subscriber<String>(){
		
		@Override
		public void onCompleted() {
			// TODO Auto-generated method stub
//			BaseToast.getInstance().ShowSystemToast(BankApplication.mInstance.getBaseContext(), "上传采集客户信息成功");
		}
		
		@Override
		public void onError(Throwable arg0) {
			// TODO Auto-generated method stub
			BaseToast.getInstance().ShowSystemToast(BankApplication.mInstance.getBaseContext(), "网络连接失败：" + arg0);
		}
		
		@Override
		public void onNext(String arg0) {
			// TODO Auto-generated method stub
//			if(!arg0.equals("") && !arg0..equals("content")){
//				getFjxxuuid(arg0);
//			}else if(!arg0.equals("") && arg0.equals("content")){
//				getFjxxuuid("");
//			}
			if(!str.equals("")){
				getFjxxuuid(str);
			}else{
				getFjxxuuid("");
			}
			
		}
		
	});
}
}
